<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理 iframe 框</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
</head>
<body>

<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list"
     style="padding: 20px 30px 0 0;">
    <input type="hidden" id="roleId"/>
    <div class="layui-form-item">
        <label class="layui-form-label">父级角色</label>
        <div class="layui-input-inline">
            <select id="roleParentId" lay-verify="required">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">所属部门</label>
        <div class="layui-input-inline">
            <select id="depId" lay-verify="required">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-inline">
            <input type="text" id="roleName" lay-verify="required" placeholder="请输入角色名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-inline">
            <input type="text" id="roleRemark" lay-verify="required" placeholder="请输入备注信息" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限</label>
        <div class="layui-input-inline eleTree authority">

        </div>
    </div>

    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="role-submit" id="role-submit"
               value="确认添加">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit"
               value="确认编辑">
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js"></script>

<link rel="stylesheet" href="../../layuiadmin/style/eleTree/eleTree.css" media="all">

<script src="../../js/jquery-1.8.3.min.js"></script>
<script>

    //下拉框初始化
    $.ajax({
        url: '/role/toAdd',
        type: 'get',
        dataType: 'json',
        data: {},
        async: false,//设置阻断
        success: function (data) {
            if (data.status != 200) {
                layer.msg('查询失败！');
            } else {
                //将父级角色下拉框中添加数据
                var modelList = data.data;
                if (modelList && modelList.length != 0) {
                    var def = "<option value='0'>顶级角色</option>";
                    $("#roleParentId").append(def);
                    for (var i = 0; i < modelList.length; i++) {
                        var option = "<option value=\"" + modelList[i].id + "\"";
                        option += ">" + modelList[i].name + "</option>";  //动态添加数据
                        $('#roleParentId').append(option);
                    }
                }
                //将所属部门下拉框中添加数据
                var modelList1 = data.depdata;
                if (modelList1 && modelList1.length != 0) {
                    for (var i = 0; i < modelList1.length; i++) {
                        var option = "<option value=\"" + modelList1[i].id + "\"";
                        option += ">" + modelList1[i].name + "</option>";  //动态添加数据
                        $('#depId').append(option);
                    }
                }
            }
        }
    });


    //layui配置
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'eleTree'], function () {
            var $ = layui.$
                , form = layui.form
                , eleTree = layui.eleTree;

            var el = eleTree.render({
                elem: '.authority',
                showCheckbox: true,
                defaultExpandAll: true,
                url: '/role/menuEleTree'
            })

            //根据父级页面判断，如果是修改，则添加默认值
            if (parent.roleType == "toUpdate") {
                var loadinit = layer.load(2, {
                    shade: [0.3, '#000'] //0.1透明度的白色背景
                });
                $.ajax({
                    url: '/role/findOne',
                    type: 'post',
                    dataType: 'json',
                    async: false,//设置阻断
                    data: {"id": parent.roleId, "parentid": 0},
                    success: function (data) {
                        $("#roleId").val(data.data.id);
                        $("#roleName").val(data.data.name);
                        $("#roleRemark").val(data.data.remark);
                        //选中下拉框中的option
                        $("#roleParentId").find("option[value=" + data.data.parentid + "]").prop("selected", true);
                        $("#depId").find("option[value=" + data.data.dep_id + "]").prop("selected", true);
                        //重新渲染下拉框
                        form.render('select');
                        //选中多选框
                        // console.log(data.menudata);
                        el.setChecked(data.menudata, true);
                        // el.reload();
                        // el.updateKeySelf(key, data);
                        layer.close(loadinit);
                    }
                });
            }

            //监听提交
            form.on('submit(role-submit)', function (data) {
                var loadmsg = layer.load(2, {
                    shade: [0.3, '#000'] //0.1透明度的白色背景
                });
                var auchorityArrays = el.getChecked(false, true);
                var field = data.field; //获取提交的字段
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                //获取id，若无则改为0
                var id = $("#roleId").val();
                if (id == null || id == "") id = 0;
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.ajax({
                    url: parent.roleURL,
                    type: 'get',
                    dataType: 'json',
                    async: false,//设置阻断
                    contentType: "application/json",
                    data: {
                        "id": id,
                        "parentid": $("#roleParentId").val(),
                        "name": $("#roleName").val(),
                        "remark": $("#roleRemark").val(),
                        "dep_id": $('#depId').val(),
                        "authoritys": JSON.stringify(auchorityArrays)
                    },
                    success: function (data) {
                        layer.close(loadmsg);
                        parent.layer.msg(data.msg, {icon: 1, zIndex: layer.zIndex + 100});
                    }
                })
                ;
                parent.layui.table.reload('roleTable'); //重载表格
                parent.layer.close(index); //再执行关闭
            });
        }
    )
</script>
</body>
</html>